﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CLibArtreve.dal;
using System.Web.Security;
using System.Collections.Specialized;
using System.Data;

namespace CLibArtreve.security
{
    public class ArtreveRoleManager : SqlRoleProvider
    {
        //CONNECTIONSTRING
        private string _connectionString;
        public string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }
        //PROPERTIES
        private RoleDAL _adapter = null;
        protected RoleDAL Adapter
        {
            get
            {
                if (_adapter == null)
                    _adapter = new RoleDAL(ConnectionString);

                return _adapter;
            }
        }
        //INITIALIZE
        public override void Initialize(string name, NameValueCollection config)
        {
            ConnectionString = config["connectionString"];
            //REMOVE connectionString FROM config
            config.Remove("connectionString");
            base.Initialize(name, config);
        }
        //ADD USERS TO ROLES
        public override void AddUsersToRoles(string[] userNames, string[] roleNames)
        {
            foreach (string userName in userNames)
            {
                foreach (string roleName in roleNames)
                {
                    try
                    {
                        int rowsAffected = Adapter.InsertUserInRole(userName, roleName);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message, ex);
                    }
                }
            }
        }
        //CREATE OR INSERT A ROLE
        public override void CreateRole(string roleName)
        {
            try
            {
                if (RoleExists(roleName))
                {
                    throw new Exception();
                }
                else
                {
                    Adapter.Insert(roleName);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        //DELETE A ROLE
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            try
            {
                int rowsAffected = Adapter.DeleteByName(roleName);
                if (rowsAffected > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        //GET ALL ROLES
        public override string[] GetAllRoles()
        {
            try
            {
                DataSet ds = Adapter.GetAll();
                //CREATE STRING ARRAY
                string[] roles = new string[ds.Tables[0].Rows.Count];
                int i = 0;
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    roles[i] = row["role_label"].ToString();
                    i++;
                }
                return roles;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        //GET ALL ROLES OF USER
        public override string[] GetRolesForUser(string userName)
        {
            try
            {
                DataSet ds = Adapter.GetByUserName(userName);
                //CREATE STRING ARRAY
                string[] roles = new string[ds.Tables[0].Rows.Count];
                int i = 0;
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    roles[i] = row["role_label"].ToString();
                    i++;
                }
                return roles;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        //CHECK IF USER IS IN ROLE
        public override bool IsUserInRole(string userName, string roleName)
        {
            try
            {
                return Adapter.IsUserInRole(userName, roleName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        //REMOVE USERS FROM ROLES
        public override void RemoveUsersFromRoles(string[] userNames, string[] roleNames)
        {
            foreach (string userName in userNames)
            {
                foreach (string roleName in roleNames)
                {
                    try
                    {
                        int rowsAffected = Adapter.DeleteUserFromRole(userName, roleName);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message, ex);
                    }
                }
            }
        }
        //CHECK IF ROLE EXISTS
        public override bool RoleExists(string roleName)
        {
            try
            {
                return Adapter.RoleExists(roleName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
    }
}
